package com.biz.hunter.dao;

import com.biz.hunter.db.entity.OrderDesc;
import com.biz.hunter.dbutil.BaseDAO;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public class OrderDescDAO {
	private static final String SQL_LIST_ALL = "select * from tb_orderdesc where isdelete=0 and istemplate=1";
	private static final String SQL_GET_ID = "select * from tb_orderdesc  where id = ?";
	private static final String SQL_UPDATE_HUNTERDESC = "update tb_hunter_desc set isdelete=1 where hunterId=? and orderdescId=?";
	private static final String SQL_UPDATE_HUNTERDESC_STATUS = "update tb_hunter_desc set status=? where hunterId=? and orderdescId=?";
	private static final String SQL_DELETE_HUNTERDESC = "delete from tb_hunter_desc where hunterId=? and orderdescId=?";
	private static final String SQL_DELETE_HUNTERDESC_BY_HUNTERID = "delete from tb_hunter_desc where hunterId=? ";
	private static final String SQL_SAVE_HUNTERDESC = "insert into tb_hunter_desc(hunterId,orderdescId,status,createTime,updateTime) VALUES(?,?,?,?,?)";
	private static final String SQL_GET_ALL_BY_HUNTERID = "select b.*,a.`status` as `status` from tb_hunter_desc a LEFT JOIN tb_orderdesc b on a.orderdescId=b.id where a.hunterId=? and a.isdelete=0 order by b.tag desc";
	private static final String SQL_SAVE = "insert into tb_orderdesc(name,spec,unit,tag,remark) VALUES(?,?,?,?,?)";
	private static final String SQL_UPDATE = "update tb_orderdesc set name=?,spec=?,unit=?,tag=?,remark=? where id=?";
	private static final String SQL_DELETE = "delete from tb_orderdesc where id=?";
	

	public OrderDesc getById(long Id) {
		return BaseDAO.queryForObject(OrderDesc.class, SQL_GET_ID, Id);
	}

	public List<OrderDesc> listAll(){
		return BaseDAO.queryForList(OrderDesc.class, SQL_LIST_ALL);
	}

	public List<OrderDesc> getAllByHunterId(int hunterId){
		return BaseDAO.queryForList(OrderDesc.class, SQL_GET_ALL_BY_HUNTERID, hunterId);
	}
	
	public int updateStatus(int status,int hunterId,int orderdescId) {
		if (hunterId <= 0) {
			return -1;
		}
		return BaseDAO.execute(SQL_UPDATE_HUNTERDESC_STATUS,status, hunterId,orderdescId);
	}

	public int update(int hunterId,int orderdescId) {
		if (hunterId <= 0) {
			return -1;
		}
		return BaseDAO.execute(SQL_UPDATE_HUNTERDESC, hunterId,orderdescId);
	}
	
	public int delete(int hunterId,int orderdescId) {
		if (hunterId <= 0) {
			return -1;
		}
		return BaseDAO.execute(SQL_DELETE_HUNTERDESC, hunterId,orderdescId);
	}
	public int save(int hunterId,int orderdescId,int status,String createTime,String updateTime ) {
		if (hunterId<=0) {
			return -1;
		}
		//name,spec,unit,tag,remark
		return BaseDAO.execute(SQL_SAVE_HUNTERDESC,hunterId,status,orderdescId,createTime,updateTime);
	}
	
	public int save(OrderDesc desc) {
		if(desc==null){
			return -1;
		}
		Long id=BaseDAO.save(SQL_SAVE, desc.getName(),desc.getSpec(),desc.getUnit(),desc.getTag(),desc.getRemark());
		return id.intValue();
	}
	
	public int update(OrderDesc desc){
		if(desc==null){
			return -1;
		}
		return BaseDAO.execute(SQL_UPDATE, desc.getName(),desc.getSpec(),desc.getUnit(),desc.getTag(),desc.getRemark(),desc.getId());
	}
	
	public int delete(int orderdescId){
		if(orderdescId<=0){
			return -1;
		}
		return BaseDAO.execute(SQL_DELETE, orderdescId);
	}

	public Integer batchSave(int hunterId, List<String> orderDescList,int status, String currentTime, String updateTime) {
		Object[][] params = new Object[orderDescList.size()][5];
		int i = 0;
		for (String orderDesc : orderDescList) {
			params[i][0] = hunterId;
			params[i][1] = orderDesc;
			params[i][2] = status;
			params[i][3] = currentTime;
			params[i][4] = updateTime;
			i++;
		}
		int[] rows = BaseDAO.batch(SQL_SAVE_HUNTERDESC, params);
		if (rows != null && rows.length > 0) {
			int total = 0;
			for (int r : rows) {
				total += r;
			}
			return total;
		} else {
			return -1; // 数据库访问异常�?
		}
	}

	public void deleteAllByHunterId(Integer hunterId) {
		BaseDAO.execute(SQL_DELETE_HUNTERDESC_BY_HUNTERID, hunterId);
	}
}
